/**
 * Created by lional on 16/10/3.
 */

jGame.Scene = cc.Scene.extend(/** @lends jGame.Scene# */{

    _controller:null,

    ctor: function(controller){
        this._super();
        this._controller = controller;
        if(this._controller && typeof this._controller["ctor"] === "function") {
            this._controller["ctor"]();
        }
        //cc.log("jGame.Scene#ctor()");
    },

    beforeEnter:function () {
        if(this._controller && typeof this._controller["beforeEnter"] === "function") {
            this._controller["beforeEnter"]();
        }
    },

    onEnter:function () {
        //this.beforeEnter();
        this._super();
        if(this._controller && typeof this._controller["onEnter"] === "function") {
            this._controller["onEnter"]();
        }
        if(this._controller && typeof this._controller["onViewEnter"] === "function") {
            this._controller["onViewEnter"](this);
        }
        this.scheduleOnce(this.afterEnter, 0);
    },

    afterEnter:function () {
        if(this._controller && typeof this._controller["afterEnter"] === "function") {
            this._controller["afterEnter"]();
        }
    },

    beforeExit:function () {
        if(this._controller && typeof this._controller["beforeExit"] === "function") {
            this._controller["beforeExit"]();
        }
    },

    onExit:function () {
        //this.beforeEnter();
        this._super();
        if(this._controller && typeof this._controller["onExit"] === "function") {
            this._controller["onExit"]();
        }
        if(this._controller && typeof this._controller["onViewExit"] === "function") {
            this._controller["onViewExit"](this);
        }
        this.scheduleOnce(this.afterExit, 0);
    },

    afterExit:function () {
        if(this._controller && typeof this._controller["afterExit"] === "function") {
            this._controller["afterExit"]();
        }
    },

    setController: function(controller){
        this._controller = controller;
    },

    getController: function(){
        return this._controller;
    },

    addView: function (node_json, control, rootView) {
        var layerNode = jGame.load(node_json, "res/");

        if(typeof control === "string")control = app.getObject(control);
        var controller = control instanceof jGame.ControllerObject?control:this._controller;
        var root = rootView;
        if(!root || !(root instanceof cc.Node)) {
            root = new jGame.Layout();
            var layoutComponent = ccui.LayoutComponent.bindLayoutComponent(root);
            layoutComponent.setStretchWidthEnabled(true);
            layoutComponent.setStretchHeightEnabled(true);
            layoutComponent.setTopMargin(0);
            layoutComponent.setBottomMargin(0);
            layoutComponent.setLeftMargin(0);
            layoutComponent.setRightMargin(0);
            this.addChild(root);
        }

        root.setName(cc.path.basename(node_json, cc.path.extname(node_json)));

        while (layerNode.node.getChildrenCount() > 0) {
            var child = layerNode.node.getChildren()[0];
            child.removeFromParent();
            root.addChild(child);
        }

        jGame.setAllChildrenController(root, controller);

        var CloseButton = jGame.findView(root, "CloseButton", undefined, jGame.Button);
        if(CloseButton)CloseButton.addClickEventListener(function (sender) {

            if(controller && typeof controller["onViewExit"] === "function") {
                controller["onViewExit"](root);
            }

            root.removeFromParent();
        });

        var ClosePanel = jGame.findView(root, "ClosePanel", undefined, jGame.Layout);
        if(ClosePanel)ClosePanel.addTouchEventListener(function (sender, type) {
            switch (type) {
                case ccui.Widget.TOUCH_ENDED:

                    if(controller && typeof controller["onViewExit"] === "function") {
                        controller["onViewExit"](root);
                    }

                    root.removeFromParent();

                    break;
            }
        });

        if(controller && typeof controller["onViewEnter"] === "function") {
            controller["onViewEnter"](root);
        }
    },

    addViewWithVisibleSize: function (layer_json, control, rootView) {
        var layerNode = jGame.load(layer_json, "res/");

        if(typeof control === "string")control = app.getObject(control);
        var controller = control instanceof jGame.ControllerObject?control:this._controller;
        var root = rootView;
        if(!root || !(root instanceof cc.Node)) {
            root = new jGame.Layout();
            var layoutComponent = ccui.LayoutComponent.bindLayoutComponent(root);
            layoutComponent.setStretchWidthEnabled(true);
            layoutComponent.setStretchHeightEnabled(true);
            layoutComponent.setTopMargin(0);
            layoutComponent.setBottomMargin(0);
            layoutComponent.setLeftMargin(0);
            layoutComponent.setRightMargin(0);
            this.addChild(root);
        }

        root.setName(cc.path.basename(layer_json, cc.path.extname(layer_json)));

        while (layerNode.node.getChildrenCount() > 0) {
            var child = layerNode.node.getChildren()[0];
            child.removeFromParent();
            root.addChild(child);
        }

        jGame.setAllChildrenController(root, controller);

        var CloseButton = jGame.findView(root, "CloseButton", undefined, jGame.Button);
        if(CloseButton)CloseButton.addClickEventListener(function (sender) {

            if(typeof controller["trace"] === "function")controller["trace"](sender);

            if(controller && typeof controller["onViewExit"] === "function") {
                controller["onViewExit"](root);
            }

            root.removeFromParent();
        });

        var ClosePanel = jGame.findView(root, "ClosePanel", undefined, jGame.Layout);
        if(ClosePanel)ClosePanel.addTouchEventListener(function (sender, type) {
            switch (type) {
                case ccui.Widget.TOUCH_ENDED:

                    if(typeof controller["trace"] === "function")controller["trace"](sender, type);
                    
                    if(controller && typeof controller["onViewExit"] === "function") {
                        controller["onViewExit"](root);
                    }

                    root.removeFromParent();

                    break;
            }
        });

        var size = cc.director.getVisibleSize();
        if(root && size){
            root.setContentSize(size.width, size.height);
            ccui.helper.doLayout(root);
        }

        if(controller && typeof controller["onViewEnter"] === "function") {
            controller["onViewEnter"](root);
        }
    },

    popView: function () {
        var count = this.getChildrenCount();
        if(count > 0) {
            var root = this.getChildren()[count-1];
            var controller = root.getController();
            if(controller && typeof controller["onViewExit"] === "function") {
                controller["onViewExit"](root);
            }
            root.removeFromParent();
        }
    },
    
    showDialog: function (dialog_json, text, control, onOKCallbackName, onCancelCallbackName, onNeutralCallbackName) {
        var dialogNode = jGame.load(dialog_json, "res/");
        if(!dialogNode.node) return;

        if(typeof control === "string")control = app.getObject(control);
        var controller = control instanceof jGame.ControllerObject?control:this._controller;
        var root = new jGame.Layout();
        var layoutComponent = ccui.LayoutComponent.bindLayoutComponent(root);
        layoutComponent.setStretchWidthEnabled(true);
        layoutComponent.setStretchHeightEnabled(true);
        layoutComponent.setTopMargin(0);
        layoutComponent.setBottomMargin(0);
        layoutComponent.setLeftMargin(0);
        layoutComponent.setRightMargin(0);
        this.addChild(root);

        root.setName(cc.path.basename(dialog_json, cc.path.extname(dialog_json)));

        while (dialogNode.node.getChildrenCount() > 0) {
            var child = dialogNode.node.getChildren()[0];
            child.removeFromParent();
            root.addChild(child);
        }

        var size = cc.director.getVisibleSize();
        if(root && size){
            root.setContentSize(size.width, size.height);
            ccui.helper.doLayout(root);
        }

        jGame.setAllChildrenController(root, controller);

        var Text = jGame.findView(root, "Text");
        var OKButton = jGame.findView(root, "OKButton", undefined, jGame.Button);
        var NeutralButton = jGame.findView(root, "NeutralButton", undefined, jGame.Button);
        var CancelButton = jGame.findView(root, "CancelButton", undefined, jGame.Button);
        var CloseButton = jGame.findView(root, "CloseButton", undefined, jGame.Button);

        if(Text) {
            Text.setString(text);
        }

        if(OKButton && onOKCallbackName) {
            OKButton.setClickCallbackName(onOKCallbackName);
        }

        if(NeutralButton && onNeutralCallbackName) {
            NeutralButton.setClickCallbackName(onNeutralCallbackName);
        }

        if(CancelButton) {
            if(onCancelCallbackName) {
                CancelButton.setClickCallbackName(onCancelCallbackName);
            }else {
                CancelButton.addClickEventListener(function (sender) {

                    if(controller && typeof controller["onViewExit"] === "function") {
                        controller["onViewExit"](root);
                    }

                    root.removeFromParent();

                });
            }
        }

        if(CloseButton)CloseButton.addClickEventListener(function (sender) {

            if(controller && typeof controller["onViewExit"] === "function") {
                controller["onViewExit"](root);
            }

            root.removeFromParent();

        });

        var ClosePanel = jGame.findView(root, "ClosePanel", undefined, jGame.Layout);
        if(ClosePanel)ClosePanel.addTouchEventListener(function (sender, type) {
            switch (type) {
                case ccui.Widget.TOUCH_ENDED:

                    if(controller && typeof controller["onViewExit"] === "function") {
                        controller["onViewExit"](root);
                    }

                    root.removeFromParent();

                    break;
            }
        });

        if(controller && typeof controller["onViewEnter"] === "function") {
            controller["onViewEnter"](root);
        }
    },
    
    showTips : function (dialog_json, text) {
        cc.log("showTips : " + text);
        var dialogNode = jGame.load(dialog_json, "res/");
        if(!dialogNode.node) return;

        var controller = this._controller;
        var root = new jGame.Layout();
        var layoutComponent = ccui.LayoutComponent.bindLayoutComponent(root);
        layoutComponent.setStretchWidthEnabled(true);
        layoutComponent.setStretchHeightEnabled(true);
        layoutComponent.setTopMargin(0);
        layoutComponent.setBottomMargin(0);
        layoutComponent.setLeftMargin(0);
        layoutComponent.setRightMargin(0);
        this.addChild(root);

        root.setName(cc.path.basename(dialog_json, cc.path.extname(dialog_json)));

        while (dialogNode.node.getChildrenCount() > 0) {
            var child = dialogNode.node.getChildren()[0];
            child.removeFromParent();
            root.addChild(child);
        }

        var duration = 1;
        if(dialogNode.action) {
            root.runAction(dialogNode.action);
            duration = dialogNode.action.getDuration()*dialogNode.action.getTimeSpeed() / cc.game.config[cc.game.CONFIG_KEY.frameRate];
            dialogNode.action.play("animation", false);
        }

        var size = cc.director.getVisibleSize();
        if(root && size){
            root.setContentSize(size.width, size.height);
            ccui.helper.doLayout(root);
        }

        jGame.setAllChildrenController(root, controller);

        var Text = jGame.findView(root, "Text");

        if(Text) {
            Text.setString(text);
        }

        var animationDone = cc.callFunc(function () {

            if(controller && typeof controller["onViewExit"] === "function") {
                controller["onViewExit"](root);
            }

            root.removeFromParent();
        }, this);

        root.runAction(cc.sequence(cc.delayTime(duration), animationDone));

        if(controller && typeof controller["onViewEnter"] === "function") {
            controller["onViewEnter"](root);
        }

        var ClosePanel = jGame.findView(root, "ClosePanel", undefined, jGame.Layout);
        if(ClosePanel)ClosePanel.addTouchEventListener(function (sender, type) {
            switch (type) {
                case ccui.Widget.TOUCH_ENDED:

                    if(controller && typeof controller["onViewExit"] === "function") {
                        controller["onViewExit"](root);
                    }

                    root.removeFromParent();

                    break;
            }
        });
    }
});
